package com.gimi.cloud.bbp.manage.service.log.impl;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.ReflectUtils;
import com.gimi.cloud.bbp.dao.dao.log.LogMasterLoginDao;
import com.gimi.cloud.bbp.dao.myMapper.core.query.QueryParam;
import com.gimi.cloud.bbp.dto.log.LogMasterLoginDTO;
import com.gimi.cloud.bbp.dto.sys.SysMasterDTO;
import com.gimi.cloud.bbp.entity.log.LogMasterLoginEntity;
import com.gimi.cloud.bbp.manage.service.log.LogMasterLoginService;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 登录日志相关接口实现类
 *
 * @author wbj
 * @version 1.0
 * @Description
 * @create 2018-01-27
 **/
@Service
public class LogMasterLoginServiceImpl implements LogMasterLoginService {
    private static final Logger logger = LoggerFactory.getLogger(LogMasterLoginServiceImpl.class);
    private LogMasterLoginDao logMasterLoginDao;
    @Autowired
    public LogMasterLoginServiceImpl(LogMasterLoginDao logMasterLoginDao) {
        this.logMasterLoginDao = logMasterLoginDao;
    }

    @Override
    public List<LogMasterLoginDTO> queryList(LogMasterLoginDTO dto, boolean isPaging) throws Exception {
        if(isPaging){
            PageHelper.startPage(dto.getPageNums(),dto.getPageSizes());
        }
        String sql ="SELECT * FROM log_master_login lg WHERE lg.is_delete = 0 AND lg.ma_id = :maId AND lg.ma_name LIKE '%'||:maName||'%'" +
                "   AND TO_DAYS(lg.add_time) >= TO_DAYS(:startAddTime) AND TO_DAYS(lg.add_time) <= TO_DAYS(:endAddTime) ORDER BY lg.add_time DESC";
        QueryParam queryParam = new QueryParam();
        queryParam.setSql(sql);
        queryParam.setParamObject(dto);
        return logMasterLoginDao.selectListDTOBySql(queryParam);
    }

    @Override
    public LogMasterLoginDTO load(LogMasterLoginDTO dto, Object id) throws Exception {
        LogMasterLoginEntity logMasterLoginEntity = logMasterLoginDao.queryBy("lg_id",id);
        if(null == logMasterLoginEntity){
            return null;
        }
        LogMasterLoginDTO logMasterLoginDTO = new LogMasterLoginDTO();
        ReflectUtils.copyPropertiesIgnoreNull(logMasterLoginEntity,logMasterLoginDTO);
        return logMasterLoginDTO;
    }

    @Override
    public LogMasterLoginDTO saveOrUpdate(LogMasterLoginDTO dto) throws Exception {
        LogMasterLoginEntity logMasterLoginEntity = null;
        if(null != dto.getLgId()){
            logMasterLoginEntity = logMasterLoginDao.queryBy("lg_id",dto.getLgId());
            if(null == logMasterLoginEntity){
                throw new MsgException(ResponseCodeEnum.NO_DATA,"未获取主键为" + dto.getMaId() + "的登录日志信息，修改失败");
            }
        }else{
            logMasterLoginEntity = new LogMasterLoginEntity();
        }
        ReflectUtils.copyPropertiesIgnoreNullAndEmptyStr(dto,logMasterLoginEntity);
        if(null != logMasterLoginEntity.getLgId()){
            logMasterLoginDao.baseUpdateByPrimaryKey(logMasterLoginEntity);
        }else{
            logMasterLoginDao.baseInsert(logMasterLoginEntity);
        }
        ReflectUtils.copyPropertiesIgnoreNullAndEmptyStr(logMasterLoginEntity,dto);
        return dto;
    }

    @Override
    public int delete(LogMasterLoginDTO dto, Object id, boolean realDel) throws Exception {
        LogMasterLoginEntity logMasterLoginEntity = logMasterLoginDao.queryBy("lg_id",id);
        if (null == logMasterLoginEntity) {
            throw new MsgException(ResponseCodeEnum.PARAMETER_EXCEPTION, "未获取到主键为" + id + "的登录日志信息,删除失败");
        }
        // 非物理删除
        if (!realDel) {
            ReflectUtils.copyPropertiesIgnoreNullAndEmptyStr(dto, logMasterLoginEntity);
            logMasterLoginEntity.setIsDelete(1);
            return logMasterLoginDao.baseUpdateByPrimaryKey(logMasterLoginEntity);
        }
        return logMasterLoginDao.deleteByPrimaryKey(id);
    }

    @Override
    public void saveMasterLoginLog(SysMasterDTO sysMasterDTO,boolean isLogin) throws Exception {
        if(null != sysMasterDTO){
            // 记录登录日志
            LogMasterLoginDTO logLoginDTO = new LogMasterLoginDTO();
            ReflectUtils.copyPropertiesIgnoreNull(sysMasterDTO,logLoginDTO);
            if(isLogin){
                logLoginDTO.setLgDescription(sysMasterDTO.getMaName()+"登录了系统");
                logLoginDTO.setLgType(1);
            }else{
                logLoginDTO.setLgDescription(sysMasterDTO.getMaName()+"退出了系统");
                logLoginDTO.setLgType(2);
            }
            logLoginDTO.setMaId(sysMasterDTO.getMaId());
            logLoginDTO.setLgIp(sysMasterDTO.getIp());
            logLoginDTO.setMaName(sysMasterDTO.getMaName());
            this.saveOrUpdate(logLoginDTO);
        }
    }
}
